package com.onlineMIS.ORM.DAO.headQ.barCodeGentor;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQInputHelp;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTag;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadqEntityTag;

@Repository
public class HeadQTagDaoImpl extends BaseDAO<HeadQTag> {
	
	@Autowired
	private HeadQTagGroupDaoImpl headQTagGroupDaoImpl;

	public List<HeadQTag> getAllCustTags() {
		List<HeadQTag> tags = new ArrayList<HeadQTag>();
		
	    HeadQTag trTag = new HeadQTag();
	    trTag.setId(0);
	    trTag.setTagName("所有标签");
	    tags.add(trTag);

	    List<HeadQTag> tagsTwo = getAll(true);
	    for (HeadQTag headQTag : tagsTwo) {
			tags.add(headQTag);
		}
	    return tags;
	    
	}
	
	public List<HeadQTag> getAllSupplierTags() {
		List<HeadQTag> tags = new ArrayList<HeadQTag>();
		
	    HeadQTag trTag = new HeadQTag();
	    trTag.setId(0);
	    trTag.setTagName("所有标签");
	    tags.add(trTag);
	    // 获取供应商标签
	    List<Integer> list = selectSupplierTags();
	    DetachedCriteria criteria = DetachedCriteria.forClass(HeadQTag.class);
	    criteria.add(Restrictions.in("id", list));	    
	    List<HeadQTag> tagsTwo = getByCritera(criteria, true); 
	    for (HeadQTag headQTag : tagsTwo) {
			tags.add(headQTag);
		}
	    return tags;
	    
	}


	public List<Integer> getAllTagIds() {
		List<Integer> list  = new ArrayList<Integer>();		
		List<HeadQTag> tags = getAll(true);
		for (HeadQTag headQTag : tags) {
			list.add(headQTag.getId());
		}
		return list;
	}



	
	/**
	 * 根据权重获取该权重的标签id集合
	 * @param sort
	 * @return
	 */
	public List<Integer> selectTagsBySort(Double sort) {
		List<Integer> list = new ArrayList<Integer>();
		List<Integer> tags = headQTagGroupDaoImpl.selectTagsBySort(sort);
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQTag.class);
		criteria.add(Restrictions.in("groupId.id", tags));
		List<HeadQTag> headQTags = getByCritera(criteria, true);
		if (headQTags != null) {
			for (HeadQTag headQTag : headQTags) {
				list.add(headQTag.getId());
			}
		}
		return list;
	}

	/**
	 * 根据分组id获取该标签id集合
	 * @param id
	 * @return
	 */
	public List<Integer> selectTagsByGroupId(int id) {
		List<Integer> list = new ArrayList<Integer>();
	
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQTag.class);
		criteria.add(Restrictions.eq("groupId.id", id));
		List<HeadQTag> headQTags = getByCritera(criteria, true);
		if (headQTags != null) {
			for (HeadQTag headQTag : headQTags) {
				list.add(headQTag.getId());
			}
		}
		return list;
	}
	
	/**
	 * 获取供应商标签id集合
	 * @param id
	 * @return
	 */
	public List<Integer> selectSupplierTags() {
		List<Integer> list = new ArrayList<Integer>();
		List<Integer> supplierGroupIds = headQTagGroupDaoImpl.selectSupplierGroupIds();
	
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQTag.class);
		criteria.add(Restrictions.in("groupId.id", supplierGroupIds));
		List<HeadQTag> headQTags = getByCritera(criteria, true);
		if (headQTags != null) {
			for (HeadQTag headQTag : headQTags) {
				list.add(headQTag.getId());
			}
		}
		return list;
	}

	/**
	 * 获取客户标签id集合
	 * @param id
	 * @return
	 */
	public List<Integer> selectCustTags() {
		List<Integer> list = new ArrayList<Integer>();
		List<Integer> custGroupIds = headQTagGroupDaoImpl.selectCustGroupIds();
	
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQTag.class);
		criteria.add(Restrictions.in("groupId.id", custGroupIds));
		List<HeadQTag> headQTags = getByCritera(criteria, true);
		if (headQTags != null) {
			for (HeadQTag headQTag : headQTags) {
				list.add(headQTag.getId());
			}
		}
		return list;
	}
	
	
	/**
	 * 标签集合排序
	 * @param tagIds
	 * @return
	 */
	public List<HeadQTag> selectByIds(List<Integer> tagIds) {
		if (tagIds.size() !=0) {
			DetachedCriteria criteria = DetachedCriteria.forClass(HeadQTag.class);
			criteria.add(Restrictions.in("id", tagIds));
			criteria.addOrder(Order.asc("groupId.id"));
			List<HeadQTag> headQTags = getByCritera(criteria, true);
			return headQTags;
		}else {
			return null;
		}
		
	}

	public List<HeadQTag> getLevelOne(Integer groupIds) {
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQTag.class);
	    criteria.add(Restrictions.eq("groupId.id", groupIds));	    
	    List<HeadQTag> tags = getByCritera(criteria, true); 
		return tags;
	}


}
